<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/11/9
  Time: 10:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../../include/head.jsp">
    <jsp:param name="title" value="录入数据"/>
</jsp:include>

<jsp:include page="../../include/tenant_header.jsp"/>

<jsp:include page="../../include/tenant_sidebar.jsp"/>
<div class="content-wrapper">
    <section class="content-header">
        <h4 class="page-title">录入数据</h4>
    </section>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <div class="form-horizontal">
                    <div class="sie-padding-top">
                        <input id="deviceId" class="hidden" value="${deviceId}"/>

                        <div class="form-group form-group-change">
                            <label class="col-md-5 sie-left-name">机床id:</label>

                            <div class="col-md-7">
                                <input id="uniqueId" class="form-control sie-input-border sie-input-uwh"
                                       value="${uniqueId}" readonly="readonly">
                            </div>
                        </div>
                        <div class="form-group form-group-change">
                            <label class="col-md-5 sie-left-name">日期:</label>

                            <div class="col-md-7">
                                <div class="sie-calendar-parent">
                                    <input type="text" class="form-control sie-input-border sie-input-uwh" id="date"
                                           value="${time}" readonly>
                                </div>
                            </div>
                        </div>
                        <c:choose>
                            <c:when test="${shifts == null || shifts.size() <= 0}">
                                <div class="form-group form-group-change">
                                    <div class="col-md-offset-5">
                                        <div style="margin-top: 20px;color:red">今日为非工作日</div>
                                        <div style="margin-top: 79px;">
                                            <a class="solid-button-orange" href="javascript:history.back(-1);">返回</a>
                                        </div>
                                    </div>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <c:forEach items="${shifts}" var="shift" varStatus="status">
                                    <div class="product"
                                         style="width:100%;margin:20px 0;border:1px solid #cdd9e1;border-radius: 10px;">
                                        <div style="padding: 10px 20px 0 20px">
                                            <span>${shift.shift_name}：</span>
                                            <input class="form-control" data-key="startTime"
                                                   style="width:10%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb;"
                                                   value="${shift.start_time}"
                                                   readonly>
                                            -
                                            <input class="form-control" data-key="endTime"
                                                   style="width:10%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb;"
                                                   value="${shift.end_time}"
                                                   readonly>
                                            <span style="color: red;margin-left: 100px">实际工作时长：${shift.time}</span>

                                            <span style="padding-left:10%">lead：</span>
                                            <input class="form-control"
                                                   style="width:15%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb;"
                                                   data-key="shiftLead"
                                            <c:if test="${shift.shift_lead != '0'}">
                                                   value="${shift.shift_lead}"
                                            </c:if>>
                                        </div>

                                        <div class="row" style="padding:30px 0 20px 50px">
                                            <div class="col-md-3 num">
                                                <span>总数：</span>
                                                <input class="form-control" data-key="num" value="${shift.product_num}"
                                                       style="width:60%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb"
                                                       readonly>
                                            </div>
                                            <div class="col-md-3 goodNum">
                                                <span>良品：</span>
                                                <input class="form-control" onkeyup="showNum(this)" data-key="good"
                                                       value="${shift.good_prod_num}"
                                                       style="width:60%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb"
                                                <c:if test="${shift.work_diff_time == 0}">
                                                       readonly
                                                </c:if>>
                                                <span class="prompt"></span>
                                            </div>
                                            <div class="col-md-3 badNum">
                                                <span>次品：</span>
                                                <input class="form-control" onkeyup="showNum(this)" data-key="bad"
                                                       value="${shift.bad_prod_num}"
                                                       style="width:60%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb"
                                                <c:if test="${shift.work_diff_time == 0}">
                                                       readonly
                                                </c:if>>
                                                <span class="prompt"></span>
                                            </div>
                                            <div class="col-md-3 rate">
                                                <span>速率：</span>
                                                <input class="form-control" data-key="rate" value="${shift.prod_rate}"
                                                       onkeyup="isDouble(this)"
                                                       style="width:60%;height:30px;display: inline-block;border-radius: 5px;border-color:#aabfcb"
                                                <c:if test="${shift.work_diff_time == 0}">
                                                       readonly
                                                </c:if>>
                                                <span>个/s</span>
                                                <span class="prompt"></span>
                                            </div>
                                        </div>
                                    </div>
                                </c:forEach>
                                <div class="form-group form-group-change">
                                    <div class="col-md-offset-5" style="margin-top: 79px;">
                                        <a id="create" class="solid-button-blue" href="javascript:;"
                                           style="margin-right: 30px">保存</a>
                                        <a class="solid-button-orange" href="javascript:history.back(-1);">取消</a>
                                    </div>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        $("#create").click(function () {

            if ($(".no").length > 0) {
                swal("输入有误!", "", "error");
                return;
            }

            var productArr = $('.product');
            var data = [];
            $.each(productArr, function (index, one) {
                var oneProduct = {};
                var oneProductArr = $(one).find("input");
                if (oneProductArr.length > 0) {
                    oneProduct["deviceId"] = $("#deviceId").val();
                    oneProduct["date"] = $("#date").val();
                    for (var i = 0; i < oneProductArr.length; i++) {
                        var key = $(oneProductArr[i]).data("key");
                        var value = $(oneProductArr[i]).val();
                        if (value == null || value == "" || value == undefined) {
                            oneProduct[key] = 0;
                        } else {
                            oneProduct[key] = value;
                        }
                    }
                    data.push(oneProduct);
                }
            });

            $.ajax({
                url: "/partner/{tenant}/api/v1/product/create".replace("{tenant}", getKeyByUrl()),
                type: "post",
                contentType: "application/json;charset=utf-8;",
                data: JSON.stringify(data),
                success: function (data) {
                    if (data.result != "SUCCESS") {
                        swal({
                            title: "<spring:message code="label.error"/>!",
                            text: data.data.startTime + "-" + data.data.endTime + "<br>" + data.message,
                            type: "error",
                            html: true,
                            showCancelButton: false,
                            showConfirmButton: true,
                            confirmButtonText: "<spring:message code="label.admin.confirm"/>",
                            animation: "slide-from-top"
                        }, function () {
                            window.location.replace(("/partner/{tenant}/device/addDate?deviceId=" + $("#deviceId").val() + "&uniqueId=" + $("#uniqueId").val() + "&time=" + $("#date").val()).replace("{tenant}", getKeyByUrl()));
                        });
                    } else {
                        swal({
                            title: "<spring:message code="label.success"/>",
                            type: "success",
                            showCancelButton: false,
                            showConfirmButton: true,
                            confirmButtonText: "确定",
                            animation: "slide-from-top"
                        }, function () {
                            window.location.replace("/partner/{tenant}/product/devices".replace("{tenant}", getKeyByUrl()));
                        });
                    }
                }
            })
        })

    });

    function showNum(_this) {
        var val = $(_this).val();
        var num = $(_this).parent().parent().children(".num").children("input");
        if (!(val.match(/^\d+$/g)) && val != "") {
            $(_this).parent().children(".prompt").addClass("no");
            return;
        } else {
            $(_this).parent().children(".prompt").removeClass("no");
        }

        var good = $(_this).parent().parent().children(".goodNum").children("input").val();
        var bad = $(_this).parent().parent().children(".badNum").children("input").val();

        if (good == "" && bad == "") {
            num.val("");
            return;
        } else if (good == "") {
            num.val(bad);
            return;
        } else if (bad == "") {
            num.val(good);
            return;
        } else {
            num.val(parseInt(good) + parseInt(bad));
        }
    }

    function isDouble(_this) {
        var val = $(_this).val();
        if (!(val.match(/^-?\d+\.?\d{0,4}$/)) && val != "") {
            $(_this).parent().children(".prompt").addClass("no");
        } else {
            $(_this).parent().children(".prompt").removeClass("no");
        }
    }


</script>
